#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, a[10001], b[10001], frac[6001];
set<vector<int>> f;
long long p = 1e9 + 7;
long long qpow(long long a, long long b) {
	long long res = 1;
	while (b) {
		if (b & 1) res = res * a % p;
		a = a * a % p;
		b >>= 1;
	}
	return res;
}

void dfs(int i, int j, vector<int> c) {
	if (i == n && j == n) {
		f.insert(c);
		return;
	}
	if (i != n) {
		vector<int> tmp = c;
		tmp.push_back(b[j]);
		dfs(i + 1, j, tmp);
	}
	if (j != n) {
		vector<int> tmp = c;
		tmp.push_back(a[i]);
		dfs(i, j + 1, tmp);
	}
}

signed main() {
	freopen("zha.in", "r", stdin);
	freopen("zha.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	if (n <= 0) return 0;
	bool alleq = 1;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		if (a[i] != i) alleq = 0;
	}
	for (int i = 1; i <= n; ++i) {
		cin >> b[i];
		if (b[i] != i) alleq = 0;
	}
	frac[0] = 1;
	for (int i = 1; i <= 6000; ++i) {
		(frac[i] = frac[i - 1] * i) %= p;
	}
	if (n <= 10) {
		dfs(1, 1, {});
		cout << f.size() << endl;
	} else if (alleq) {
		--n;
		cout << frac[2 * n] * qpow(frac[n], p - 2) % p * qpow(frac[n], p - 2) % p * qpow(n + 1, p - 2) % p << endl;
	} else {
		--n;
		cout << frac[2 * n] * qpow(frac[n], p - 2) % p * qpow(frac[n], p - 2) % p << endl;
	}
	return 0;
}
